﻿// HAARDlg.cpp: 实现文件
//

#include "pch.h"
#include "3180101584SX1.h"
#include "3180101584SX1Dlg.h"
#include "HAARDlg.h"
#include "afxdialogex.h"

extern OpenImageClass OImageC;
extern CMy3180101584SX1Dlg *MainDlg;
// HAARDlg 对话框

IMPLEMENT_DYNAMIC(HAARDlg, CDialog)

HAARDlg::HAARDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DIALOG_HAAR, pParent)
{

}

HAARDlg::~HAARDlg()
{
}

void HAARDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CHECK_CONVERSE, m_check);
}


BEGIN_MESSAGE_MAP(HAARDlg, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_RUN, &HAARDlg::OnBnClickedButtonRun)
	ON_BN_CLICKED(IDC_BUTTON_JZLB, &HAARDlg::OnBnClickedButtonJzlb)
	ON_BN_CLICKED(IDC_RADIO_JZ, &HAARDlg::OnBnClickedRadioJz)
	ON_BN_CLICKED(IDC_RADIO_GS, &HAARDlg::OnBnClickedRadioGs)
	ON_BN_CLICKED(IDC_RADIO_RH, &HAARDlg::OnBnClickedRadioRh)
	ON_BN_CLICKED(IDC_BUTTON_LAYOUT, &HAARDlg::OnBnClickedButtonLayout)
	ON_BN_CLICKED(IDC_BUTTON_JJLB, &HAARDlg::OnBnClickedButtonJjlb)
	ON_BN_CLICKED(IDC_BUTTON_ADD, &HAARDlg::OnBnClickedButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_SMBH, &HAARDlg::OnBnClickedButtonSmbh)
	ON_BN_CLICKED(IDC_BUTTON_XFANZHUAN, &HAARDlg::OnBnClickedButtonXfanzhuan)
	ON_BN_CLICKED(IDC_BUTTON_YFANZHUAN, &HAARDlg::OnBnClickedButtonYfanzhuan)
	ON_BN_CLICKED(IDC_BUTTON_FANZHUAN, &HAARDlg::OnBnClickedButtonFanzhuan)
	ON_BN_CLICKED(IDC_BUTTON_90FANZHUAN, &HAARDlg::OnBnClickedButton90fanzhuan)
	ON_BN_CLICKED(IDC_BUTTON_270FANZHUAN, &HAARDlg::OnBnClickedButton270fanzhuan)
	ON_BN_CLICKED(IDC_BUTTON1, &HAARDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &HAARDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &HAARDlg::OnBnClickedButton4)
END_MESSAGE_MAP()


// HAARDlg 消息处理程序


void HAARDlg::OnBnClickedButtonRun()
{
	CString sLevel;
	GetDlgItemText(IDC_EDIT_LEVEL, sLevel);
	if (sLevel == "")
		return;
	int LevelNum = _ttoi(sLevel);
	int tt = m_check.GetCheck();
	if (tt == 1)
	{
		m_check.SetCheck(0);
	}
	else if(tt == 0)
	{
		m_check.SetCheck(1);
	}
	OImageC.MyHaar_8_BitTransform(OImageC.etmImg->Rimg, OImageC.pImgW, OImageC.pImgH, LevelNum, tt);
	OImageC.MyHaar_8_BitTransform(OImageC.etmImg->Gimg, OImageC.pImgW, OImageC.pImgH, LevelNum, tt);
	OImageC.MyHaar_8_BitTransform(OImageC.etmImg->Bimg, OImageC.pImgW, OImageC.pImgH, LevelNum, tt);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


BOOL HAARDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CWnd* pWnd = AfxGetMainWnd();     //获取主窗口指针

	CDC * pDC = pWnd->GetDC();
	hdc = pDC->GetSafeHdc();

	GetDlgItem(IDC_STATIC_BL)->SetWindowText(_T("1"));

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void HAARDlg::OnBnClickedButtonJzlb()
{
	CString sNum;
	GetDlgItemText(IDC_STATIC_BL, sNum);
	double Beilv = atof(sNum);
	GetDlgItemText(IDC_EDIT_11, sNum);
	double Num11 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_12, sNum);
	double Num12 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_13, sNum);
	double Num13 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_21, sNum);
	double Num21 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_22, sNum);
	double Num22 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_23, sNum);
	double Num23 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_31, sNum);
	double Num31 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_32, sNum);
	double Num32 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_33, sNum);
	double Num33 = atof(sNum) / Beilv;

	OImageC.Image_SmoothConv2_RGBFunc(OImageC.etmImg, Num11, Num12, Num13, Num21, Num22, Num23, Num31, Num32, Num33,TRUE);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedRadioJz()
{
	GetDlgItem(IDC_EDIT_11)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_12)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_13)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_21)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_22)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_23)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_31)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_32)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_33)->SetWindowText(_T("1"));
	GetDlgItem(IDC_STATIC_BL)->SetWindowText(_T("9"));
}


void HAARDlg::OnBnClickedRadioGs()
{
	GetDlgItem(IDC_EDIT_11)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_12)->SetWindowText(_T("2"));
	GetDlgItem(IDC_EDIT_13)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_21)->SetWindowText(_T("2"));
	GetDlgItem(IDC_EDIT_22)->SetWindowText(_T("4"));
	GetDlgItem(IDC_EDIT_23)->SetWindowText(_T("2"));
	GetDlgItem(IDC_EDIT_31)->SetWindowText(_T("1"));
	GetDlgItem(IDC_EDIT_32)->SetWindowText(_T("2"));
	GetDlgItem(IDC_EDIT_33)->SetWindowText(_T("1"));
	GetDlgItem(IDC_STATIC_BL)->SetWindowText(_T("16"));
}


void HAARDlg::OnBnClickedRadioRh()
{
	GetDlgItem(IDC_EDIT_11)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_EDIT_12)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_EDIT_13)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_EDIT_21)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_EDIT_22)->SetWindowText(_T("9"));
	GetDlgItem(IDC_EDIT_23)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_EDIT_31)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_EDIT_32)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_EDIT_33)->SetWindowText(_T("-1"));
	GetDlgItem(IDC_STATIC_BL)->SetWindowText(_T("1"));
}


void HAARDlg::OnBnClickedButtonLayout()
{
	long int BlackNum;
	CString sBlackNum;
	long int WhiteNum;
	CString sWhiteNum;
	GetDlgItemText(IDC_EDIT_BLACK2, sBlackNum);
	GetDlgItemText(IDC_EDIT_WHITE2, sWhiteNum);
	if (sBlackNum == "")
	{
		BlackNum = 0;
	}
	else
	{
		BlackNum = _ttoi(sBlackNum);
	}
	if (sWhiteNum == "")
	{
		WhiteNum = 0;
	}
	else
	{
		WhiteNum = _ttoi(sWhiteNum);
	}

	OImageC.SaltPepperNoise_RGBFunc(OImageC.etmImg, WhiteNum, BlackNum);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);

}


void HAARDlg::OnBnClickedButtonJjlb()
{
	CString sNum;
	GetDlgItemText(IDC_STATIC_BL, sNum);
	double Beilv = atof(sNum);
	GetDlgItemText(IDC_EDIT_11, sNum);
	double Num11 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_12, sNum);
	double Num12 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_13, sNum);
	double Num13 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_21, sNum);
	double Num21 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_22, sNum);
	double Num22 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_23, sNum);
	double Num23 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_31, sNum);
	double Num31 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_32, sNum);
	double Num32 = atof(sNum) / Beilv;
	GetDlgItemText(IDC_EDIT_33, sNum);
	double Num33 = atof(sNum) / Beilv;

	OImageC.Image_SmoothConv2_RGBFunc(OImageC.etmImg, Num11, Num12, Num13, Num21, Num22, Num23, Num31, Num32, Num33, FALSE);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButtonAdd()
{
	CString FileName;

	LPCTSTR szFilter = _T("000 files(*.00)|*.000|");		//提示保存文件的类型
	CFileDialog dlg(FALSE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilter);		// CFileDialog的第一个参数标明是浏览(打开，TRUE)还是保存（FALSE）

	if (dlg.DoModal() == IDOK)		//返回值是OK，说明点击了“保存”/“确认”按钮，返回值是IDCANCEL，说明点击了取消
	{
		FileName = dlg.GetPathName();		//保存文件的路径到filename变量中
	}

	if (FileName == "")
	{
		MessageBox("保存文件路径为空！");
		return;
	}

	OImageC2.etmImg = (ETMIMG *)malloc(sizeof(ETMIMG));

	OImageC2.ReadEtm000File_Func(FileName);

	OImageC2.etmImg->EdRmin = OImageC2.Rmin;
	OImageC2.etmImg->EdRmax = OImageC2.Rmax;
	OImageC2.etmImg->EdGmin = OImageC2.Gmin;
	OImageC2.etmImg->EdGmax = OImageC2.Gmax;
	OImageC2.etmImg->EdBmin = OImageC2.Bmin;
	OImageC2.etmImg->EdBmax = OImageC2.Bmax;

	const char * B000name = NULL;
	int Widt = OImageC2.pImgW;
	int Heig = OImageC2.pImgH;

	OImageC2.Gene_Char_Etmimg_MemorySpace(Heig, Widt, OImageC2.etmImg);

	OImageC2.ReadEtmFileData_Func(OImageC2.etmImg, B000name);

	OImageC3.etmImg = (ETMIMG *)malloc(sizeof(ETMIMG));

	OImageC3 = OImageC;

	MainDlg->Invalidate();
	MainDlg->SendMessageA(WM_PAINT);

	OImageC.Image_Add_Func(OImageC2.etmImg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButtonSmbh()
{
	CString FileName;

	LPCTSTR szFilter = _T("000 files(*.00)|*.000|");		//提示保存文件的类型
	CFileDialog dlg(FALSE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilter);		// CFileDialog的第一个参数标明是浏览(打开，TRUE)还是保存（FALSE）

	if (dlg.DoModal() == IDOK)		//返回值是OK，说明点击了“保存”/“确认”按钮，返回值是IDCANCEL，说明点击了取消
	{
		FileName = dlg.GetPathName();		//保存文件的路径到filename变量中
	}

	if (FileName == "")
	{
		MessageBox("保存文件路径为空！");
		return;
	}
	
	OImageC.ReadEtm000File_Func(FileName);
	
	FileName = FileName.Left(FileName.GetLength() - 4);

	CString Name4 = FileName + "_B4.img";
	CString Name5 = FileName + "_B5.img";
	CString Name6 = FileName + "_B6.img";
	CString Name7 = FileName + "_B7.img";

	FILE *rfo4 = fopen(Name4, "rb");
	FILE *rfo5 = fopen(Name5, "rb");
	FILE *rfo6 = fopen(Name6, "rb");
	FILE *rfo7 = fopen(Name7, "rb");

	CString NameSB = FileName + "_SB.txt";
	CString NameGV = FileName + "_GV.txt";
	CString NameIY = FileName + "_IY.txt";
	CString NameIN = FileName + "_IN.txt";

	FILE *wfoSB = fopen(NameSB, "wb");
	FILE *wfoGV = fopen(NameGV, "wb");
	FILE *wfoIY = fopen(NameIY, "wb");
	FILE *wfoIN = fopen(NameIN, "wb");

	OImageC.ReadEtm_Data_UChar_Func(rfo4, OImageC.pImgH, OImageC.pImgW, OImageC.etmImg->Rimg);
	OImageC.ReadEtm_Data_UChar_Func(rfo5, OImageC.pImgH, OImageC.pImgW, OImageC.etmImg->Gimg);
	OImageC.ReadEtm_Data_UChar_Func(rfo6, OImageC.pImgH, OImageC.pImgW, OImageC.etmImg->Bimg);
	OImageC.ReadEtm_Data_UChar_Func(rfo7, OImageC.pImgH, OImageC.pImgW, OImageC.etmImg->Mimg);

	OImageC.MssTasseledCapKT4x4_Func(OImageC.etmImg, OImageC.etmImg->Rimg, OImageC.etmImg->Gimg, OImageC.etmImg->Bimg, OImageC.etmImg->Mimg, wfoSB, wfoGV, wfoIY, wfoIN);

	AfxMessageBox("Done");
}


void HAARDlg::OnBnClickedButtonXfanzhuan()
{
	OImageC.Bitmap_X_Miror_RGBFunc(OImageC.etmImg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButtonYfanzhuan()
{
	OImageC.Bitmap_Y_Miror_RGBFunc(OImageC.etmImg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButtonFanzhuan()
{
	OImageC.Bitmap_X_Miror_RGBFunc(OImageC.etmImg);
	OImageC.Bitmap_Y_Miror_RGBFunc(OImageC.etmImg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButton90fanzhuan()
{
	OImageC.Bitmap_Rota90DFunc(OImageC.etmImg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButton270fanzhuan()
{
	OImageC.Bitmap_Rota270Func(OImageC.etmImg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButton1()
{
	OImageC.Middle(OImageC.etmImg, OImageC.etmImg->Rimg);
	OImageC.Middle(OImageC.etmImg, OImageC.etmImg->Gimg);
	OImageC.Middle(OImageC.etmImg, OImageC.etmImg->Bimg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButton3()
{
	OImageC.Big(OImageC.etmImg, OImageC.etmImg->Rimg);
	OImageC.Big(OImageC.etmImg, OImageC.etmImg->Gimg);
	OImageC.Big(OImageC.etmImg, OImageC.etmImg->Bimg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}


void HAARDlg::OnBnClickedButton4()
{
	OImageC.Small(OImageC.etmImg, OImageC.etmImg->Rimg);
	OImageC.Small(OImageC.etmImg, OImageC.etmImg->Gimg);
	OImageC.Small (OImageC.etmImg, OImageC.etmImg->Bimg);
	OImageC.GencRemoColorImgFunc(OImageC.etmImg, OImageC.etmImg->FastWidth, OImageC.etmImg->FastHeight);
	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
}
